// pages/tabList/tabList.js
const app = getApp()
var postLogin = require("../../utils/wh.js");
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    enterprise: [],
    latitude: '',
    longitude: '',
    title:"测试",
    markers: [{
      id: 1,
      latitude: '',
      longitude: '',
      commpany_name: "",//公司名称
    }],

    infos: [],
    limit_num: 6,//每頁數量
    idxpage: 1,//第幾頁
    honor: [], // 企业荣誉
    // tab切换  
    currentTab: 0,
    // 人才招聘
    list: [],//人才招聘列表,
    scroll_top: 0,
     },
  go: function () {
    var that = this ;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度  
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var latitudeIdx = Number(that.data.latitude);
        var longitudeIdx = Number(that.data.longitude);
        var commpany_name = that.data.markers.commpany_name;
        wx.openLocation({
          latitude: latitudeIdx,
          longitude: longitudeIdx,
          name: commpany_name,
          scale: 28
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var optionsID = options.id;
    if (!optionsID) {
      this.setData({
        currentTab: Number(0)
      })
    }
    if (optionsID == 3) {

      this.setData({
        currentTab: Number(optionsID)
      })
    }
    wx.showLoading({
      title: '加载中•••',
      // mask: true
    })
    this.loop();
  },
  onShow: function (options) {
    var idx = Number(app.globalData.info_id);
    if (idx == 1) {
      console.log(1)
      this.setData({
        currentTab: Number(idx)
      })
    }
    // 首页传值
    app.globalData.info_id = '';

  },
  loop: function () {
    var that = this;
    var code = app.globalData.code;
    var url = app.globalData.URL;
    if (!code || !url) {
      var that = this
      setTimeout(function () { that.loop(); }, 100)
    } else {
      that.setData({
        code: code,
        url: url,
      })
      var postUrl = '/api/company.index/introduce',//公司简介
        postData = {};//公司简介参数
      var enterpriseUrl = '/api/company.index/news',//企业咨询
        enterpriseData = {
          page: that.data.idxpage,
          limit_num: that.data.limit_num,
          catid: "",
        };//企业咨询参数
      var honorUrl = '/api/company.index/honor',//公司荣誉
        honor = {};//公司荣誉
      var recruitmentUrl = '/api/company.index/job',//人才招聘
        recruitment = {};//人才招聘
      postLogin.Post(postUrl, postData, that.enterprise);//公司简介
      postLogin.Post(enterpriseUrl, enterpriseData, that.consulting);//企业咨询
      postLogin.Post(honorUrl, honor, that.honor);//公司荣誉
      postLogin.Post(recruitmentUrl, recruitment, that.recruitment);//人才招聘
    }
  },
  enterprise: function (res) {
    let that = this;
    wx.hideLoading();
    var markers = this.data.markers;
        markers.latitude = res.lat;
        markers.longitude = res.lng;
        markers.commpany_name = res.commpany_name;
    that.setData({
      enterprise: res,
      partner: res.album,
      latitude: res.lat,
      longitude: res.lng,
      markers: markers
    })
  },
  consulting: function (res) {
    let that = this;
    this.setData({
      infos: res.list
    })

  },
  honor: function (res) {
    let that = this;
    this.setData({
      honor: res
    })
  },
  recruitment: function (res) {
    let that = this;
    for (var i = 0, len = res.length; i < len; ++i) {
      res[i]['open_status'] = false;
      res[i]['select_status'] = false;
    }
    /**
    * WxParse.wxParse(bindName , type, data, target,imagePadding)
    * 1.bindName绑定的数据名(必填)
    * 2.type可以为html或者md(必填)
    * 3.data为传入的具体数据(必填)
    * 4.target为Page对象,一般为this(必填)
    * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
    */
    for (let j = 0; j < res.length; j++) {
      WxParse.wxParse('reply' + j, 'html', res[j].job_content, that)
      if (j === res.length - 1) {
        WxParse.wxParseNewArray("list", 'reply', res.length, that,res)
      }
    }
  },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    var that = this;
    this.goTopFun();
    that.setData({ currentTab: e.detail.current });
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // 资讯详情
  goDetail: function () {
    // var id = e.currentTarget.id;
    // console.log(id);
    // wx.navigateTo({
    //   url: '../newsDetail/newsDetail?news_id=' + id
    // })
    app.globalData.info_id = '1';
  },
  // 人才招聘展开收起
  kindToggle: function (e) {
    var id = e.currentTarget.id;//当前id
    var list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].job_id == id) {
        list[i].open_status = !list[i].open_status,
          list[i].select_status = !list[i].select_status
      } else {
        list[i].open_status = false,
          list[i].select_status = false
      }
    }
    // list[id].open_status = !list[id].open_status
    this.setData({
      list: list,
    });
  },
  // 公司荣誉预览图片
  previewImg: function () {
    var urls = this.data.honor;
    // console.log(urls);
    var imgUrls = Array();
    for (var i = 0, len = urls.length; i < len; ++i) {
      imgUrls.push(urls[i].honor_src);
    }
    console.log(imgUrls);
    wx.previewImage({
      current: 'current', // 当前显示图片的http链接
      urls: imgUrls // 需要预览的图片http链接列表
    })
    app.globalData.info_id = '2';
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
    }
    return {
      title: '人才招聘',
      path: 'pages/aboutus/aboutus?id=3',
      success: function () {
        // console.log('我是'+that.data.currentTab)
      },
      fail: function () {
        // 转发失败
      }
    }
  },
  scrollTopFun: function (e) {
    var idxTop = e.detail.scrollTop ;
    this.setData({
        scrollTop: idxTop,
      });
     
  },
  goTopFun: function (e) {
    var _top = this.data.scroll_top;//发现设置scroll-top值不能和上一次的值一样，否则无效，所以这里加了个判断 
    console.log(this.data.scrollTop)
    if (_top == 1) {
      _top = 0;
    } else {
      _top = 1;
    }
    this.setData({
      scrollTop: 0,
    });
    console.log("----");
    console.log(this.data.scrollTop)
  }
})